package com.jinhua.batch.utils;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.jinhua.batch.model.ProgressVO;

//import com.tencent.sr.rmall.springbootstarter.redis.RedisClient;
//import com.tencent.sr.rmall.springbootstarter.utils.JsonUtils;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

import com.redis.RedisCache;
import org.apache.commons.lang3.StringUtils;
import org.redisson.client.RedisClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2023/3/31
 */
public class ProgressUtil {
    private static final Logger log = LoggerFactory.getLogger(ProgressUtil.class);

//    private static RedisClient redisClient = (RedisClient)SpringContextUtil.getBean("redisClient", RedisClient.class);
    static RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
    private static int redisExpireTime = (int)Duration.ofDays(30L).getSeconds();

    public ProgressUtil() {
    }

    public static void saveProgress(ProgressVO progressVO) {
        log.info("正在上报状态给redis，当前状态为progressVO={}", JSONUtil.toJsonStr(progressVO));
        String key = RedisKeyUtil.getUploadTaskKey(progressVO.getTaskId());
//        redisClient.setex(key, JsonUtils.toString(progressVO), redisExpireTime);
        redisCache.setCacheObject(key, JSONUtil.toJsonStr(progressVO), redisExpireTime, TimeUnit.SECONDS);
    }

    public static ProgressVO queryTaskStatus(String queryKey) {
        String key = RedisKeyUtil.getUploadTaskKey(queryKey);
//        String queryResult = redisClient.get(key);
        String queryResult = redisCache.getCacheObject(key);
        if (StringUtils.isEmpty(queryResult)) {
//            throw new TRetailRuntimeException("TASK_INFO_EXPIRE", "任务处理进度的信息已经超过查看期限，当前已无法查看任务进度。");
            return null;
        } else {
            ProgressVO progressVO = (ProgressVO)JSON.parseObject(queryResult, ProgressVO.class);
            return progressVO;
        }

    }
}

